package com.example.mybatisplus.web.controller;

import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Movie;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.UserService;
import com.example.mybatisplus.model.domain.User;

import java.util.Objects;


/**
 *
 *  前端控制器
 *
 *
 * @author fmx
 * @since 2024-11-10
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private UserService userService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        User  user =  userService.getById(id);
        return JsonResponse.success(user);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        userService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateUser(User  user) throws Exception {
        userService.updateById(user);
        return JsonResponse.success(null);
    }

    @PostMapping("login")
    // @ResponseBody
    public JsonResponse login(@RequestBody User user){
        User user1 = userService.login(user);
        return JsonResponse.success(user1);
    }

    @GetMapping("logout")
    // @ResponseBody
    public JsonResponse logout(){
        SessionUtils.clearCurUser();
        return JsonResponse.success(true);
    }


    /**
    * 描述:创建User
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse register(User  user) throws Exception {
        if(user.getUserPassword()==null|user.getUserName()==null){return JsonResponse.success(false);}
        else {boolean b =userService.save(user);
        return JsonResponse.success(b);}
    }

    @PostMapping("addUser")
    public boolean addUser(@RequestBody User user) {
        if(userService.getByUserName(user))
        {return false;}
        else
        {Boolean b=userService.save(user);
            return b;}
    }

    @PostMapping("editUser")
    public boolean editUser(@RequestBody User user){
        if(Objects.equals(userService.getById(user.getUserId()).getUserName(), user.getUserName())){
            return userService.updateById(user);
        }
        if(userService.getByUserName(user))
        {return false;}
        else return userService.updateById(user);
    }


}

